package com.hand.wfive.adapter;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import org.jetbrains.annotations.Nullable;

import java.util.List;

public abstract class BaseCheckPositionAdapter<T, VH extends BaseViewHolder>
        extends BaseQuickAdapter<T, VH> {

    protected int baseCheckPosition;

    public BaseCheckPositionAdapter(int layoutResId) {
        super(layoutResId);
    }

    public BaseCheckPositionAdapter(int layoutResId, @Nullable List<T> data) {
        super(layoutResId, data);
    }

    public boolean updateCheckPosition(int position) {
        if (baseCheckPosition == position) {
            return false;
        }
        if (position > getItemCount() - 1) {
            return false;
        }
        int oldPosition = baseCheckPosition;
        baseCheckPosition = position;
        notifyItemChanged(oldPosition);
        notifyItemChanged(baseCheckPosition);
        return true;
    }

    public void updateCheckData(T t) {
        if (baseCheckPosition >= 0) {
            setData(baseCheckPosition, t);
        }
    }

    public int getBaseCheckPosition() {
        return baseCheckPosition;
    }

    public T checkData() {
        if (baseCheckPosition >= 0 && baseCheckPosition < getDefItemCount()) {
            return getItem(baseCheckPosition);
        }
        return null;
    }
}
